import store from '../store';
import adminLayout from '@/layout/admin/index';
import commonLayout from '@/layout/admin/common';
import mainLayout from '@/layout/admin/main';
class formatDynamicRouter {
	constructor(router, menus) {
		let adminRouter = [{
			path: '/admin',
			name: 'Admin',
			redirect: '/approval',
			meta: {
				title: '首页'
			},
			component: adminLayout,
			children: this.eachMenuData(router, menus)
		}]
		return adminRouter;
	}

	eachMenuData(router, arr) {
		// let dataArr = [];
		arr.forEach(item => {
			let obj = { ...item };
			if (!!obj.children && obj.children.length > 0) {
				obj['redirect'] = obj.children[0].path;
				if (obj.meta.level === '1') {
					obj['component'] = commonLayout;
				} else if (obj.meta.level === '2') {
					obj['component'] = mainLayout;
				} else if (obj.meta.level === '3') {
					obj['component'] = () => import(`@/views/admin${obj.path}/index`);
				}
				this.eachMenuData(router, obj.children);
			} else {
				obj['component'] = () => import(`@/views/admin${obj.path}/index`);
			}
			router.addRoute(obj);
			// dataArr.push(obj);
		})
		// return dataArr;
	}
}

export default (router, menus) => {
	new formatDynamicRouter(router, menus)
}